#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int n;
const int N = 35;
int pre[N], mid[N];
const int M = 1000;
int e[M], ne[M], h[M], idx;
int root;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int dfs(int preL, int preR, int midL, int midR)
{
    if (midL == midR) return mid[midL];
    auto pit = find(mid, mid + n, pre[preL]);
    int pos = pit - &mid[0];
    int lsize = pit - &mid[midL];
    int rsize = &mid[midR] - pit;
    // cout << preL << ' ' << preL + lsize << ' ' << midL <<  ' ' << pos - 1 << endl;
    // cout << preL + lsize + 1 <<  ' ' << preR << ' ' << pos + 1 << ' '<< midR << endl;
    int nextL = -1, nextR = -1;
    if (lsize)
        nextL = dfs(preL + 1, preL + lsize, midL, pos - 1);
    if (rsize)
        nextR = dfs(preL + lsize + 1, preR, pos + 1, midR);
    if (nextL != -1) add(*pit, nextL);
    if (nextR != -1) add(*pit, nextR);
    return *pit;
}

void bfs(int root)
{
    queue<int> q;
    q.push(root);
    bool first = true;
    while (q.size())
    {
        int tt = q.front();
        cout << tt;
        first = false;
        q.pop();
        for (int i = h[tt]; ~i; i = ne[i])
            q.push(e[i]);
        if (q.size()) cout << ' ';
    }
}

int main()
{
    memset(h, -1, sizeof h);
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> mid[i];
    for (int i = 0; i < n; ++i) cin >> pre[i];
    root = pre[0];
    dfs(0, n - 1, 0, n - 1);
    bfs(root);
    return 0;
}